package top.xizai.ezpoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @NAME: WSC
 * @DATE: 2021/12/28
 * @DESCRIBE:
 **/
public class EZPOITest {
    public static void main(String[] args) throws IOException {
        //新建测试数据
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setName("周芷若" + i);
            user.setAge(18 + i);
            user.setBirthday(new Date());
            user.setSex(new Random().nextInt(2));
            userList.add(user);
        }
        /*---------------------下面是使用EasyPoi导出的核心方法----------------------------*/
        // ExportParams() 两个参数，一个是title 表格标题, 一个是sheetName
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户表","sheet1"),User.class,userList);
        //导出文件到指定位置
        FileOutputStream outputStream = new FileOutputStream("D:\\WorkSpace\\easy-poi\\src\\main\\resources\\用户表.xls");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
}
